var mongoose = require('../index');
var SaleProduct = require('./SaleProduct');

const config = require('./config');
var Stringify = mongoose.Schema.Formatter.Stringify
var ObjectId = mongoose.Schema.Types.ObjectId

mongoose.connect(config.mysql, { });

let id = '102';

var product = {
    _id: id,
    test: {
        a: '1122221',
        b: '2222333',
    },
    providerId: 1,// {_id: 1, providerAbbr: '中荷人寿', providerCode: 'ZHRS', id: 1},
    providerName: 'ZHRS:中荷人寿',
    icpCode: '203100',
    productAbbr: '百年康多保重疾险2222',
    planRiderType: '1',
    planAttrType: '1',
    basicLawType: '1',
    statisticsType: '2',
    productName: '百年人寿康多保重大疾病险',
    productCode: 'BNKDBZJX',
    sagencyfee: null,
    eagencyfee: null,
    productType: '1',
    issueType: '3',
    accountName: 'account1',
    realSalesVolume: 0,
    branch: '86',
    saleStartDate: '2019-02-28T13:27:58.000Z',
    saleEndDate: '2080-01-01T13:27:58.000Z',
    publishDate: '2019-02-28',
    productStatus: '1',
    hesitatePeriod: '10',
    visitDay: null,
    claimDatum: null,
    claimRemark: null,
    orderNo: 10000,
    externalProductId: '',
    externalProducttName: '',
    createdAt: '2019-02-28T13:31:28.000Z',
    updatedAt: '2019-02-28T13:31:28.000Z',
    branchCode: ['86', '8601'],
    correctTerm: ['8', '10', '11', '12', '13', '98', '99'],
    feeType: ['101', '102', '103', '301', '1302'],
    safePeriod: ['1y', '5y', '15y', '101'],
};

let data = {
    $push: {
        branchCode: { $each: product.branchCode },
        correctTerm: { $each: product.correctTerm },
    },
    $set: {
        productStatus: '1',
    },
};

SaleProduct.findOne({_id: id}, function(err, product) {
    if (err) {
        console.log(err);
    }
    if (!product) {
        console.log(product);
    }

    // right
    /*product.test.a = 'a0305';
    product.test.b = 'b0305';*/

    // wrong
    product.test = {
        a: 'a030501',
        b: 'b030501',
    };

    product.save(function(err) {
        if (err) {
            console.log(err);
        }
        console.log('ok');
    })
});

/*SaleProduct.find({}, null, {
    populate:[
        { path: 'providerId', select: '' },
        { path: 'parties.partiesId', select: '' },
    ]
}, function(err, results) {
    console.log(err);
    console.log(JSON.stringify(results));
});*/

let page = 1;
let options = {
    page: page,
    limit: 10,
    populate: [
        { path: 'providerId', select: '' },
        { path: 'parties.partiesId', select: '' },
    ],
    // populate: 'providerId parties.partiesId',
    sort: { createdAt: -1 },
};
SaleProduct.paginate({}, options).then((result) => {
    for (let i = 0; i < result.docs.length; i++) {
        console.log(result.docs[i].providerId);
    }
    /*var model = {
        isAdmin: true,
        product: result.docs,
        page: page,
        pageCount: result.pages,
        total: result.total,
    };
    console.log(JSON.stringify(model));*/
});

return;

/*
SaleProduct.update({_id: id}, data, function(err) {
    if (err) {
        console.log(err);
    }
    console.log('ok');
});*/

let model = new SaleProduct(product);

model.save(function(err) {
    if (err) {
        console.log(err);
    }
    console.log('ok');
});
